<template>
  <ContentWrap>
    <fromTitle title="标准维护" :hasPermi="'qis:standard-maintain:query'"  :queryParams="queryParams" @search="handleSearch" @reset="handleReset"/> 
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="标准名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入标准名称"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="标准编号" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入标准编号"
          clearable
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="标准分类" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择标准分类"
          clearable
          class="!w-240px"
        >
          <el-option label="国家标准" value="0" />
          <el-option label="行业标准" value="1" />
          <el-option label="中国石油企业标准" value="2" />
        </el-select>
      </el-form-item>

      <el-form-item label="标准状态" prop="state">
        <!-- <el-input
          v-model="queryParams.state"
          placeholder="请输入标准状态"
          clearable
          class="!w-240px"
        /> -->
        
        <el-select
          v-model="queryParams.state"
          placeholder="请选择标准状态"
          clearable
          class="!w-240px"
        >
          <el-option label="有效" value="0" />
          <el-option label="废止" value="1" />
          <el-option label="被替代" value="2" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="发布时间" prop="publishDate">
        <el-date-picker
          v-model="queryParams.publishDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetime"
         placeholder="请选择发布时间"
          class="!w-240px"
        />
      </el-form-item> -->
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
      <el-button type="warning" v-hasPermi="['qis:standard-maintain:create']"  @click="openForm('create')">新增</el-button>
      <el-button type="info" v-hasPermi="['qis:standard-maintain:export']" @click="handleExport">导出</el-button>
      <el-button type="danger" v-hasPermi="['qis:standard-maintain:delete']" @click="handleDelelte">删除</el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox v-model="scope.row.selected" @change="handleSelect(scope.row)"/>
        </template>
      </el-table-column>
      <el-table-column label="标准编号" width="180px" align="center" prop="num" />
       
      <el-table-column label="标准名称"  align="center" prop="name" >
        <template #default="scope">
          <el-link type="primary" @click="handleUpdate(scope.row.id)">
            {{ scope.row.name }}
          </el-link>
        </template>
      </el-table-column>


        <el-table-column label="标准分类" width="120px" align="center" prop="type" >
        <template #default="scope">
          <span v-if="scope.row.type == 0">国家标准</span>
          <span v-if="scope.row.type == 1">行业标准</span>
          <span v-if="scope.row.type == 2">中国石油企业标准</span>
        </template>
        </el-table-column>
      <el-table-column
        label="发布日期"
        align="center"
        prop="publishDate"
        width="180px"
      />
      <el-table-column
        label="实施日期 "
        align="center"
        prop="implementDate"
        width="180px"
      />
      <el-table-column label="发布单位"  align="center" prop="publishCompany" />
      <el-table-column label="标准状态" align="center" >
        <template #default="scope">
          <span v-if="scope.row.state == 0">有效</span>
          <span v-if="scope.row.state == 1">废止</span>
          <span v-if="scope.row.state == 2">被替代</span>

        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->

    </el-table>
    <!-- 分页 -->
    <div>
      <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- <el-button type="primary" @click="getList()">确认</el-button> -->
    </div>
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { StandardMaintainApi, StandardMaintainVO } from '@/api/qis/standardmaintain'
import { useRouter } from 'vue-router'
import { useMessage } from '@/hooks/web/useMessage'
import { useI18n } from '@/hooks/web/useI18n'
import { useAppStore } from '@/store/modules/app'
import download from '@/utils/download'
import { ElMessage, ElMessageBox } from 'element-plus'
/** 质检平台-标准维护 列表 */
defineOptions({ name: 'StandardMaintain' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()
const appStore = useAppStore()

const loading = ref(true) // 列表的加载中
const list = ref<StandardMaintainVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive<any>({
  pageNo: 1,
  pageSize: 10,

})
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StandardMaintainApi.getStandardMaintainPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleSearch = () => {
  getList()
}

const arrayList = ref([])
const handleSelect = (row ) => {

  // console.log('删除指标要求', row)
  if(row.selected){
    arrayList.value.push(row.id)
    console.log(arrayList.value,'arrayList.value')
  }else {
    arrayList.value = arrayList.value.filter(item => item !== row.id)
    console.log(arrayList.value,'arrayList.value')
  }
}
/** 重置按钮操作 */
const handleReset = () => {
  queryFormRef.value.resetFields()
  getList()
}

/** 添加/修改操作 */
const openForm = (type?: string, id?: number) => {
  appStore.setBreadcrumb(false)
  router.push({
    path: '/InspectionInfoManagement1/StandardMaintenance/form',
    query: {
      type: type || 'create',
      id: id?.toString()
    }
  })
}

// const handleExport = async () => {
//   await StandardMaintainApi.exportStandardMaintain(queryParams)
// }

const exportLoading = ref(false)

/** 导出按钮操作 */
const handleExport = async () => {
  if(total.value > 10000) return message.error('当前数据过大，请筛选数据后导出')
  try {
   if(arrayList.value.length > 0) {
    const idsArr = arrayList.value.join(';') 
        // 导出的二次确认
        await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await StandardMaintainApi.exportStandardMaintain({ids: idsArr,...queryParams})
    download.excel(data, '质检平台-标准维护.xls')
   }else {
        // 导出的二次确认
        await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await StandardMaintainApi.exportStandardMaintain(queryParams)
    download.excel(data, '质检平台-标准维护.xls')
   }


  } catch {
  } finally {
    exportLoading.value = false
  }
}

const handleUpdate = (id: number) => {
  appStore.setBreadcrumb(false)
  router.push({
    path: '/InspectionInfoManagement1/StandardMaintenance/form',
    query: {
      type: 'update',
      id: id.toString()
    }
  })
}

const handleDelelte = async (id:number)=>{
  // arrayList.value = []

  if(arrayList.value.length > 0){
  ElMessageBox.confirm('确定删除选中数据吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
   
      const stringIds =  arrayList.value.join(';')
   console.log(stringIds,'stringIds')
   await StandardMaintainApi.deleteStandardMaintain(stringIds)
   arrayList.value = []
   message.success('删除成功')
   getList()
   list.value.forEach(e=>{
    e.selected  = false
    
   })
  
  }).catch(() => {
  })

}else {
      message.error('请选择要删除的数据')
    }

  
  // try {
  //   // 删除的二次确认
  //   await message.delConfirm()
  //   // 发起删除
  //   await StandardMaintainApi.deleteStandardMaintain(id)
  //   message.success(t('common.delSuccess'))
   
  //   // 刷新列表
  //   await getList()
  // } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.table-btn{
  padding: 10px 50px;
}
</style>